<template>
  <div class="musicList">
    <!-- 推荐歌单头部 -->
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>

    <!-- 推荐歌单列表 -->
    <div class="musicContent">
      <van-swipe class="my-swpie" :loop="false" :width="130" :show-indicators="false">
        <van-swipe-item v-for="item in state.musicList" :key="item.name" @click="jumpMusic(item.id)">
          <img :src="item.picUrl">

          <!-- 播放量 -->
          <span class="playCount">
            <van-icon name="service-o" />
            {{changeCount(item.playCount)}}
          </span>
          <span class="name">{{ item.name }}</span>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>

</template>

<script>
import { getMusicList } from '@/api/home.js'
import { reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
// 转变播放量事件js文件
import { changeCount } from '@/utils/changeCount.js'
export default {
  name: 'music',
  setup() {
    // 定义推荐歌单数据
    const state = reactive({
      musicList: []
    })
    // 跳转歌单详情事件
    const router = useRouter()
    function jumpMusic(musicId) {
      router.push({ path: '/itemMusic', query: { id: musicId } })
    }
    // 请求推荐歌单数据
    onMounted(async () => {
      const { data: res } = await getMusicList()
      if (res.code !== 200) {
        Toast.fail('获取推荐歌单失败')
      }
      // 请求回来的推荐歌单数据赋值，进行渲染操作
      state.musicList = res.result
    })

    return { state, changeCount, jumpMusic }
  }
}
</script>

<style lang="less" scoped>
.musicList {
  width: 100%;
  height: 5rem;
  padding: 0.2rem;
  .musicTop {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.2rem;
    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }
    .more {
      border: 1px solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }
  .musicContent {
    width: 100%;
    height: 3.6rem;

    .van-swipe-item {
      //   margin-right: 0.14rem;
      padding-right: 0.2rem;
      position: relative;
      height: 3.8rem;
      img {
        width: 100%;
        height: 2.4rem;
        border-radius: 0.2rem;
        //   position: absolute;
      }
      .playCount {
        position: absolute;
        z-index: 100;
        right: 0.3rem;
        color: white;
        margin-top: 0.06rem;
      }
      .name {
        //   position: absolute;
        bottom: 0px;
      }
    }
  }
}
</style>